<?php

/**
 * @desc 请求ERP
 * @author zhangweifeng@shopex.cn 15-7-7 上午11:02
 */
class apierp_erp_rpc {

    private $_api_url = '';
    private $_flag = '';  // erp 标识
    private $_token = '';  // erp 私钥
    private $_timeout = 10;
    public $task_name = '';

    function __construct() {
        if(!file_exists(ROOT_DIR.'/config/certi.php')) {
            echo "config/certi.php文件不存在";
            exit;
        }
        require(ROOT_DIR.'/config/certi.php');
        if(!isset($request_api_config['token']) || !isset($request_api_config['url'])) {
            echo "config/certi.php文件中必须定义request_api_config数组，数组中必须包括token和url";
            exit;
        }            
        $this->_token = $request_api_config['token'];
        $this->_api_url = $request_api_config['url'];
         
    }
    /**
     * @desc ：调用oubao的进销存api
     * @author jeff 2015/09/29 16:11:38
     * @param params
     * @return 返回值类型
    */
    function call_api_yucai($post) {
        $url = $this->_api_url;
        $post['params']['token'] = md5($this->_token);
        $res = $this->_http_post($url, $post); 
        return $res;
    }

    /**
     * @desc 生成签名
     * @param type $params
     * @param type $token
     * @return boolean
     */
    private function gen_sign($params, $token) {
        if (!$token) {
            return false;
        }
        return strtoupper(md5(strtoupper(md5($this->assemble($params))) . $token));
    }

    private function assemble($params) {
        if (!is_array($params)) {
            return null;
        }
        ksort($params, SORT_STRING);
        $sign = '';
        foreach ($params AS $key => $val) {
            if (is_null($val)) {
                continue;
            }
            if (is_bool($val)) {
                $val = ($val) ? 1 : 0;
            }
            $sign .= $key . (is_array($val) ? $this->assemble($val) : $val);
        }
        return $sign;
    }
     /**
     * @desc http 进行api  请求
     * @param type $url 请求地址
     * @param type $request_data 请求的参数
     * @param type $params 应用级参数
     * @return boolean
     */
    private function _http_post($url, $request_data, $params=null, $rpc_id=null) {
   
        $this->soap=new SoapClient($url); 
        if (empty($this->soap)) {
            exit('soap connect error');
        }   
    
        $sGUID=$this->createGUID();
        $dtMake=date('Y-m-d H:i:s');  
        $strNote='';
        $strStoreId='';
        $strUserno='';
        $accessKey=$this->soap->GetAccessKey(array($sGUID,$dtMake,$strNote,$strStoreId,$strUserno));
        if (!empty($accessKey) && $accessKey->GetAccessKeyResult == true) {
            $sResult = $this->objectToArray(json_decode($accessKey->sResult));
            $des = kernel::service('apierp.rpc_erp_des');
            //本地没有DES扩展包，线上有，所以可以直接调用就可以了
            $des->CryptDes("ENJOYCRM");
            // $des = new Des("ENJOYCRM");//（秘钥向量，混淆向量）

            $this->strKey=$des->decrypt($sResult['<strKey>k__BackingField']);
            $result=$this->soap->$request_data['method']($request_data['params']);
            $res=$this->objectToArray($result);
            return $res;
        }    
    }

    /**
     * ps ：生成随机编码
     * Time：2016/01/25 17:26:24
     * @author zhangyan
     * @param 参数类型
     * @return 返回值类型
    */
    function createGUID(){
        $str = md5(uniqid(mt_rand(), true));   
        $uuid  = substr($str,0,8) . '-';   
        $uuid .= substr($str,8,4) . '-';   
        $uuid .= substr($str,12,4) . '-';   
        $uuid .= substr($str,16,4) . '-';   
        $uuid .= substr($str,20,12);   
        return $prefix . $uuid;
    }
    /**
     * ps ：对象转换为数组
     * Time：2016/01/25 17:27:00
     * @author zhangyan
     * @param 参数类型
     * @return 返回值类型
    */
    function objectToArray($e){
        $e=(array)$e;
        foreach($e as $k=>$v){
            if( gettype($v)=='resource' ) return;
            if( gettype($v)=='object' || gettype($v)=='array' )
                $e[$k]=(array)objectToArray($v);
        }
        return $e;
    }
    
    /**
     * 超时时间设置
     */
    public function set_timeout($timeout) {
        $this->_timeout=$timeout;
    }
   

}
